package cn.itcast.eduorder.controller;


import cn.itcast.commonutils.R;
import cn.itcast.eduorder.service.PayLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-06-20
 */
@RestController
@RequestMapping("/eduorder/paylog")
@CrossOrigin
public class PayLogController {
    @Autowired
    private PayLogService payLogService;

    @ApiOperation(value = "获取支付二维码")
    @GetMapping("/getPayQrCode/{orderNO}")
    public R getPayQrCode(@PathVariable String orderNO){
        Map<String,Object> map= payLogService.getPayQrCode(orderNO);

        return R.ok().data(map);
    }

    @ApiOperation(value = "根据订单号查询订单状态")
    @GetMapping("/queryPayStatus/{orderNO}")
    public R queryPayStatus(@PathVariable String orderNO){
        Map map = payLogService.queryPayStatus(orderNO);
        if (map == null) {
            return R.error().message("支付出错了");
        }
//        System.out.println("-----查询支付结果-----");
//        System.out.println(map);
        //如果返回map里面不为空，通过map获取订单状态
        if (map.get("trade_state").equals("SUCCESS")){ // 支付成功
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }

        return R.ok().code(25000).message("支付中...");
    }
}

